package com.aabte.design2.behavioral.observer;

import com.google.common.collect.LinkedHashMultimap;
import com.google.common.collect.Multimap;

import java.util.Collection;

/**
 * @author Daniel
 * @version 1.0
 * @date 2020/5/4
 */
public class EventManager {

    private Multimap<String, EventListener> listeners = LinkedHashMultimap.create();

    public void subscribe(String eventType, EventListener listener) {
        this.listeners.put(eventType, listener);
    }

    public void unsubscribe(String eventType, EventListener listener) {
        this.listeners.remove(eventType, listener);
    }

    public void eventNotify(String eventType, Object data) {
        Collection<EventListener> eventListeners =
                this.listeners.get(eventType);
        for (EventListener eventListener : eventListeners) {
            eventListener.eventNotify(data);
        }
    }
}
